﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Abm_Empresa
{
    public partial class frm_empresas : Form
    {
        public frm_empresas()
        {
            InitializeComponent();
        }

        private void frm_empresas_Load(object sender, EventArgs e)
        {

        }

        private void btn_limpiar_Click(object sender, EventArgs e)
        {
            gb_filtrosBusqueda.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Text = String.Empty);
            gb_filtrosBusqueda.Controls.OfType<ComboBox>().ToList().ForEach(comboBox => comboBox.Text = String.Empty);
        }

        private void btn_buscar_Click(object sender, EventArgs e)
        {
            BaseDeDatos baseDeDatos = BaseDeDatos.Instance;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "LOS_GDDES.SP_BUSCAR_EMPRESA";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            if (txt_razonSocialEmpresa.Text != "")
                sqlCommand.Parameters.AddWithValue("@razon_social", txt_razonSocialEmpresa.Text);
            if (txt_emailEmpresa.Text != "")
                sqlCommand.Parameters.AddWithValue("@email", txt_emailEmpresa.Text);
            if (txt_cuitEmpresa.Text != "")
                sqlCommand.Parameters.AddWithValue("@cuit", txt_cuitEmpresa.Text);
            
            DataTable dt_empresas = baseDeDatos.consulta(sqlCommand);
            dgv_empresas.DataSource = dt_empresas;

            dgv_empresas.Columns[1].Visible = false;
            dgv_empresas.Columns[2].Visible = false;
        }

        private void dgv_empresas_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            btn_modificar.Enabled = true;
            btn_eliminar.Enabled = true;         
        }

        private void btn_modificar_Click(object sender, EventArgs e)
        {
            frm_modificarEmpresa frm_modificarEmpresa = new frm_modificarEmpresa(dgv_empresas.CurrentRow);
            frm_modificarEmpresa.ShowDialog();
            this.btn_buscar_Click(sender, e);
        }

        private void btn_crear_Click(object sender, EventArgs e)
        {
            frm_crearEmpresa frm_crearEmpresa = new frm_crearEmpresa();
            frm_crearEmpresa.ShowDialog();
            this.btn_buscar_Click(sender, e);
        }

        private void btn_eliminar_Click(object sender, EventArgs e)
        {
            DataRow empresa = (dgv_empresas.CurrentRow.DataBoundItem as DataRowView).Row;

            BaseDeDatos baseDeDatos = BaseDeDatos.Instance;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "LOS_GDDES.SP_USUARIO_X_ROL_BAJA_LOGICA";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("id_usuario", empresa.Field<Decimal>("ID_USUARIO"));
            sqlCommand.Parameters.AddWithValue("id_rol", 2);
            baseDeDatos.ejecuta(sqlCommand);
            this.btn_buscar_Click(sender, e);
        }
    }
}
